#!/bin/bash
# 执行：sh git_url_to_gitlab_api_url.sh http://aaa.gitlab.com/feg/fea-testing.git
# 结果：http://aaa.gitlab.com/api/v4/projects/feg%2Ffea-testing
cd `dirname "$0"`

if [ $# -ne 2 ] ;then
  echo "本脚本用于将通过gitlab的http的git地址和token 取项目id"
  echo "使用sh git_url_to_gitlab_api_url.sh 某某.git 某某token"
  exit
fi

gitlab_git_real_address=$1
gitlab_token=$2

# aaa.com
gitlab_com_url=`echo $gitlab_git_real_address | cut -d/ -f3`

# 项目路径
project_path=`echo $gitlab_git_real_address | cut -d/ -f4`

# 项目名称
gitlab_project_name=`echo $gitlab_git_real_address | cut -d/ -f5 | cut -d. -f1`

# 转换后的api地址
# gitlab_api_url=http://$gitlab_com_url/api/v4/projects/$project_path%2F$gitlab_project_name


project_id=`curl -XGET -H "Content-Type: application/json" --header "PRIVATE-TOKEN: $gitlab_token" http://$gitlab_com_url/api/v4/projects/$project_path%2F$gitlab_project_name | python -c 'import sys, json; print(json.load(sys.stdin)["id"])'`
echo $project_id
